package springboot_rabbitmq.config;


import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author 帅小伙
 * @date 2022/1/7
 * @description  备份交换机
 * 一个备份交换机 一个警告交换机
 * 警告交换机主要的职责处理消息
 */
@Configuration
public class BackupSwitch {
    public static final String CONFIRM_EXCHANGE_NAME = "confirm1.exchange";
    public static final String CONFIRM_QUEUE_NAME = "confirm1.queue";
    public static final String CONFIRM_EXCHANGE_KEY = "key1";
    public static final String BACKUP_EXCHANGE_NAME = "backup.exchange";
    public static final String BACKUP_QUEUE_NAME = "backup.queue";
    public static final String WARNING_QUEUE_NAME = "warning.queue";


    // 备门市队列
    @Bean("confirmQueue1")
    public Queue  confirmQueue(){
        return QueueBuilder.nonDurable(CONFIRM_QUEUE_NAME).build();
    }

    // 门市交换机
    @Bean("confirmExchange1")
    public DirectExchange confirmExchange(){
        ExchangeBuilder builder =ExchangeBuilder.
                directExchange(CONFIRM_EXCHANGE_NAME)
                .durable(false)
                //设置备份交换机
                .withArgument("alternate-exchange",BACKUP_EXCHANGE_NAME);
        return (DirectExchange)builder.build();
    }

    // 门市绑定
    @Bean("queueBinding1")
    public Binding queueBinding(@Qualifier("confirmQueue1") Queue queue, @Qualifier("confirmExchange1")DirectExchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(CONFIRM_EXCHANGE_KEY);
    }



    // 备份队列
    @Bean("backupQueue")
    public Queue  backupQueue(){
        return QueueBuilder.nonDurable(BACKUP_QUEUE_NAME).build();
    }


    // 备份交换机
    @Bean("backupExchange")
    public FanoutExchange backupExchange(){
        return new FanoutExchange(BACKUP_EXCHANGE_NAME);
    }

    // 绑定 备份交换机 和 备份
    @Bean
    public Binding backupBinding(@Qualifier("backupQueue") Queue queue, @Qualifier("backupExchange")FanoutExchange exchange) {
        return BindingBuilder.bind(queue).to(exchange);
    }

    // 警告队列
    @Bean("warningQueue")
    public Queue  warningQueue(){
        return QueueBuilder.nonDurable(WARNING_QUEUE_NAME).build();
    }


    // 绑定 备份交换机 和  警告队列
    @Bean
    public Binding waringBinding(@Qualifier("warningQueue") Queue queue, @Qualifier("backupExchange")FanoutExchange exchange) {
        return BindingBuilder.bind(queue).to(exchange);
    }








}
